package com.gensdai.leliang.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseActivityNoAbs;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/21.
 */

public class tuya extends BaseActivityNoAbs implements View.OnTouchListener {

    @BindView(R.id.huabu)
    ImageView hb;
    Canvas canvas;
    Paint paint;

    float downX;
    float downY;
    float upX;
    float upY;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuya);
        ButterKnife.bind(this);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
//
        BitmapDrawable bd = (BitmapDrawable) hb.getBackground();
        Bitmap map = bd.getBitmap();
        Bitmap maps = Bitmap.createBitmap(map.getWidth(), map.getHeight(), map.getConfig());
//        Bitmap maps = createViewBitmap(hb);
//        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.app_logo).copy(Bitmap.Config.ARGB_8888, true);
        canvas = new Canvas(maps);
        paint = new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.STROKE);
        Matrix matrix = new Matrix();
        canvas.drawBitmap(maps, matrix, paint);

        hb.setImageBitmap(maps);
        hb.setOnTouchListener(this);//设置监听

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("touch_draw", "ontouch()");

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
//          upX = event.getX();
//          upY = event.getY();//画直线的相关代码，可自己测试。同理，画圆椭圆都可在相关方法里设置
//          canvas.drawLine(downX, downY, upX, upY, paint);
//          mImageView.invalidate();
//          break;
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                upX = event.getX();
                upY = event.getY();
                canvas.drawLine(downX, downY, upX, upY, paint);
                hb.invalidate();
                downX = upX;
                downY = upY;
                break;
            case MotionEvent.ACTION_CANCEL:
                break;

        }
        return true;
    }

    public Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

}